class TimeNode extends CoolNode
{
	private m_timelabels:Array<egret.TextField>;
	private m_timeCount:number;
	private m_timer:egret.Timer;
	private m_Callback:Function;
	private m_CallbackObj:egret.DisplayObjectContainer;
	private m_Background:egret.Bitmap;
	public constructor(callback:Function,callbackObj:egret.DisplayObjectContainer) 
	{
		super();
		this.m_Callback = callback;
		this.m_CallbackObj = callbackObj;
		this.initTimer();
		this.initTimeBg();
		this.initTimeLabel();
	}
	public setTime(time:number)
	{
		this.m_timeCount = time;
		this.setTimeLabelText();
	}
	public setTimerStart()
	{
		this.m_timer.start();
	}
	public setTimerStop()
	{
		this.m_timer.stop();
	}
	private initTimer()
	{
		this.m_timeCount = 0;
		this.m_timer = new egret.Timer(1000, 0);
		this.m_timer.addEventListener(egret.TimerEvent.TIMER,this.timerFunc,this);
	}
	private initTimeBg()
	{
		let timeBg = this.createBitmapByName("shijian");
		timeBg.alpha = 0.8;
		timeBg.anchorOffsetX = timeBg.width / 2;
		timeBg.anchorOffsetY = timeBg.height / 2;
		timeBg.x = this.width - timeBg.width / 2 - timeBg.height / 6;
		timeBg.y = timeBg.height / 3 * 2;
		this.addChild(timeBg);
		this.m_Background = timeBg;
	}
	private initTimeLabel()
	{
		let xArr = [64,26,-24,-62];
		this.m_timelabels = new Array<egret.TextField>();
		for(let i = 0; i < 4; i++)
		{
			let timelabel = new egret.TextField();
			timelabel.size = 40;
			timelabel.verticalAlign = egret.VerticalAlign.MIDDLE;
			timelabel.text = "0";
			timelabel.anchorOffsetX = timelabel.width / 2;
			timelabel.anchorOffsetY = timelabel.height / 2;
			timelabel.x = this.m_Background.x - xArr[i];
			timelabel.y = this.m_Background.y - 4;
			this.addChild(timelabel);
			this.m_timelabels.push(timelabel);
		}
		this.setTimeLabelText();
	}
	private timeEndCallback()
	{
		let bindFunction = this.m_Callback.bind(this.m_CallbackObj);
		bindFunction(event);
	}
	private timerFunc()
    {
        this.m_timeCount--;
		this.setTimeLabelText();
		if(this.m_timeCount <= 0)
		{
			this.timeEndCallback();
		}
    }
	private setTimeLabelText()
	{
		let value:string = this.m_timeCount.toString();
		for(let i = 0; i < this.m_timelabels.length; i++)
		{
			this.m_timelabels[i].text = "0";
		}
		for(let i = 0; i < value.length; i++)
		{
			this.m_timelabels[this.m_timelabels.length - i - 1].text = value[value.length - i - 1];
		}
	}
}